﻿using Adnc.Application.Shared;
using Adnc.Application.Shared.Dtos;
using Adnc.WebApi.Shared;
using Microsoft.AspNetCore.Mvc;
using WYT.Sys.Application.Contracts.Dtos.RedNews;
using WYT.Sys.Application.Contracts.Services;

namespace WYT.Sys.WebApi.Controllers
{
    /// <summary>
    /// 小红书服务
    /// </summary>
    [Route("sys/[Controller]")]
    [ApiController]
    public class RedNewsController : AdncControllerBase
    {
        private readonly IRedNewsAppService _redNewsAppService;

        private readonly IUserContext _userContext;
        public RedNewsController(IRedNewsAppService redNewsAppService
            , IUserContext userContext)
        {
            _redNewsAppService = redNewsAppService;
            _userContext = userContext;
        }

        /// <summary>
        /// 获取分组
        /// </summary>
        /// <returns></returns>
        [HttpGet("Group")]
        public async Task<WYTActionResult<List<RedNewsGroupDto>>> GetRedNewsGroup()
        {
            return Success(await _redNewsAppService.GetRedNewsGroup());
        }

        /// <summary>
        /// 用户分享
        /// </summary>
        /// <param name="input">小红书ID </param>
        /// <returns></returns>
        [HttpPost("Shared")]
        public async Task<WYTActionResult<bool>> SharedAsync(SimpleDto<long> input)
        {
            return Success(await _redNewsAppService.SharedAsync(_userContext.Id, input.Value));
        }

        /// <summary>
        /// 用户收藏
        /// </summary>
        /// <param name="input">小红书ID</param>
        /// <returns></returns>
        [HttpPost("Favorite")]
        public async Task<WYTActionResult<bool>> FavoriteAsync(SimpleDto<long> input)
        {
            return Success(await _redNewsAppService.FavoriteAsync(_userContext.Id, input.Value));
        }

        /// <summary>
        /// 取消收藏
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("CancelFavorit")]
        public async Task<WYTActionResult<bool>> CancelFavoriteAsync(SimpleDto<long> input)
        {
            return Success(await _redNewsAppService.CancelFavoriteAsync(_userContext.Id, input.Value));
        }

        /// <summary>
        /// 获取用户收藏列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("FavoriteList")]
        public async Task<WYTActionResult<PageModelDto<RedNewsListDto>>> GetUserFavorite([FromQuery]SearchPagedDto input)
        {
            return Success(await _redNewsAppService.GetUserFavorite(_userContext.Id, input));
        }

        /// <summary>
        /// 用户点赞
        /// </summary>
        /// <param name="input">小红书ID</param>
        /// <returns></returns>
        [HttpPost("Praised")]
        public async Task<WYTActionResult<bool>> PraisedAsync(SimpleDto<long> input)
        {
            return Success(await _redNewsAppService.PraisedAsync(_userContext.Id, input.Value));
        }

        /// <summary>
        /// 获取小红书
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<WYTActionResult<PageModelDto<RedNewsListDto>>> GetRedNews([FromQuery] RedNewsSearchPagedDto input)
        {
            return Success(await _redNewsAppService.GetRedNews(input));
        }

        /// <summary>
        /// 根据Id获取小红书
        /// </summary>
        /// <param name="input">小红书ID</param>
        /// <returns></returns>
        [HttpGet("detail")]
        public async Task<WYTActionResult<RedNewsDetailDto>> GetRedNewsByID([FromQuery]SimpleDto<long> input)
        {
            return Success(await _redNewsAppService.GetRedNewsByID(_userContext.Id, input.Value));
        }

        /// <summary>
        /// 获取评论
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("Comments")]
        public async Task<WYTActionResult<PageModelDto<RedNewsCommentsListDto>>> GetComments([FromQuery]SearchPagedDto<long> input)
        {
            return Success(await _redNewsAppService.GetComments(input));
        }

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("WriteComments")]
        public async Task<WYTActionResult<bool>> PublishComments(RedNewsCommentsAddDto input)
        {
            return Success(await _redNewsAppService.PublishComments(_userContext.Id ,input));
        }

        /// <summary>
        /// 添加小红书
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<WYTActionResult<bool>> AddRedNews(RedNewsAddDto input)
        {
            if (input == null)
                return new WYTActionResult<bool>(400, "参数错误",false);
            if(input.PictureAddress.Count == 0)
                return new WYTActionResult<bool>(400, "至少需要上传一张图片", false);

            return Success(await _redNewsAppService.AddRedNews(_userContext.Id,input));
        }

        /// <summary>
        /// 获取用户自己发表的小红书
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("SelfList")]
        public async Task<WYTActionResult<PageModelDto<RedNewsSimpleDot>>> GetUserRedNews([FromQuery]SearchPagedDto input)
        {
            return Success(await _redNewsAppService.GetUserRedNews(_userContext.Id, input));
        }
    }
}
